<template>
  <div v-show="edit">
    <table-where :forms="forms"></table-where>
    <wms-table ref="tableQuery" :tableDatas="tableDatas" @editHandle="editHandle"></wms-table>
  </div>
  <div v-show="!edit">
    <table-where :forms="editForms"></table-where>
  </div>
</template>

<script setup name="往来单位">
import { ref, reactive } from 'vue'
import { POSTJSON } from '@/components/utils/http'
import { useWmsGlobal } from '@/store/wmsGlobal'
import pinyin from 'js-pinyin'
const wmsGlobal = useWmsGlobal()
// import { createFromJson } from '@/components/utils/api'
const edit = ref(true)

const tableDatas = reactive({
  body: {
    //表数据
    head: 'clients',
    url: '/Information/getClientsPage',
    params: {},
    type: 'POSTJSON'
  },
  delete: {
    url: '/Information/deleteClients',
    params: { id: 'id' }
  },
  edit: true
})
const tableQuery = ref()

const onSubmit = () => {
  tableQuery.value.getData()
}
const forms = reactive({
  data: tableDatas.body.params,
  rules: [],
  compValue: [
    { label: '编号', value: 'company_number', type: 'iconinput', placeholder: 'asd', show: true, onclick: () => {} },
    { label: '名称', value: 'name', type: 'iconinput' },
    {
      label: '新增',
      type: 'button',
      onclick: () => {
        edit.value = false
      }
    },
    { label: '查询', type: 'button', onclick: onSubmit }
  ]
})
const createPinyin = (value)=>{
  if(!value)return;
  pinyin.setOptions({checkPolyphone: false, charCase: 1}) 
  editForms.data.pinyin = pinyin.getCamelChars(value)
}
const editForms = reactive({
  data: {},
  rules: [],
  compValue: [
    { label: '编号', value: 'client_number', type: 'input', placeholder: '编号', show: true, rule: { required: false } },
    { label: '名称', value: 'name', type: 'input', placeholder: '名称', show: true, rule: { required: true },change:createPinyin },
    { label: '拼音', value: 'pinyin', type: 'input', placeholder: '拼音', show: true, rule: { required: false } },
    { label: '客户类型', value: 'client_type', type: 'select',options: wmsGlobal.wmsOptions['client_type'] },
    { label: '城市', value: 'city', type: 'input', placeholder: '城市', show: true, rule: { required: false } },
    { label: '地址', value: 'address', type: 'input', placeholder: '地址', show: true, rule: { required: false } },
    { label: '联系人', value: 'person', type: 'input', placeholder: '联系人', show: true, rule: { required: false } },
    { label: '电话', value: 'tel', type: 'input', placeholder: '电话', show: true, rule: { required: false } },
    { label: '邮件', value: 'email', type: 'input', placeholder: '邮件', show: true, rule: { required: false } },
    { label: '备注', value: 'note', type: 'input', placeholder: '备注', show: true, rule: { required: false } },
    {
      label: '保存',
      type: 'button',
      onclick: () => {
        POSTJSON('/Information/addOrUpdateClients', editForms.data).then(data => {
          if (data.code > 0) {
            editForms.data = {}
          }
        })
      }
    },
    {
      label: '返回',
      type: 'button',
      onclick: () => {
        edit.value = true
      }
    }
  ]
})
const editHandle = ({ row }) => {
  edit.value = false
  editForms.data = Object.assign({}, row)
}
</script>
